﻿using System;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Xml.Linq;
using System.Data;
using System.Data.SqlClient;
using System.Threading;
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
// [System.Web.Script.Services.ScriptService]
public class Service : System.Web.Services.WebService
{
    public Service()
    {
        //Uncomment the following line if using designed components 
        //InitializeComponent(); 
    }

    [WebMethod]
    public string HelloWorld()
    {
        return "Hello World";
    }

    private SqlConnection getConnection()
    {
        SqlConnection conn = new SqlConnection(@"Data Source=DATTRAN-PC\SQLEXPRESS;Initial Catalog=ttcb1;Integrated Security=True;Pooling=False");
        return conn;
    }

    [WebMethod(Description = "Them Chuyen Bay")]
    public void Search3Chang(string loai, string diemDi, string ngayDi, string thangDi, string diemDen, string ngayVe, string thangVe, string diemtrunggian, string ngaytrunggian, string thangtrunggian)
    {
        Airplane.lKhuHoi.Clear();
        Airplane.lInfo.Clear();
        Airplane.lcuoi.Clear();
        XoaCB();
        XoaCB3();
        XoaCBKH();
        ViNaAirlines vina = new ViNaAirlines();
        ViNaAirlines vina1 = new ViNaAirlines();
        ViNaAirlines vina2 = new ViNaAirlines();
        MekongAirlines mk = new MekongAirlines();
        vina.init(loai, diemDi, ngayDi, thangDi, diemDen, ngayVe, thangVe);
        vina1.init(loai, diemDi, ngayDi, thangDi, diemDen, ngayVe, thangVe);
        vina2.init(loai, diemDi, ngayDi, thangDi, diemDen, ngayVe, thangVe);
        mk.init(loai, diemDi, ngayDi, thangDi, diemDen, ngayVe, thangVe);
        try { vina1.TimKiem1(ngayDi, thangDi, diemDi, ngayVe, thangVe, diemDen); }
        catch (Exception) { };
        try { vina2.TimKiem2(ngayVe, thangVe, diemDen, ngaytrunggian, thangtrunggian, diemtrunggian); }
        catch (Exception) { };
        try { vina.TimKiem3(ngaytrunggian, thangtrunggian, diemtrunggian, ngayDi, thangDi, diemDi); }
        catch (Exception) { };
        try { mk.TimKiem(ngayDi, thangDi, diemDi, ngayVe, thangVe, diemDen, Airplane.lInfo); }
        catch (Exception) { };
        try { mk.TimKiem(ngayVe, thangVe, diemDen, ngaytrunggian, thangtrunggian, diemtrunggian, Airplane.lKhuHoi); }
        catch (Exception) { };
        try { mk.TimKiem(ngaytrunggian, thangtrunggian, diemtrunggian, ngayDi, thangDi, diemDi, Airplane.lcuoi); }
        catch (Exception) { };

    }
    [WebMethod(Description = "Them Chuyen Bay")]
    public void SearchAll(string loai, string diemDi, string ngayDi, string thangDi, string diemDen, string ngayVe, string thangVe)
    {
        Airplane.lKhuHoi.Clear();
        Airplane.lInfo.Clear();
        XoaCB();
        XoaCBKH();
        ViNaAirlines vina = new ViNaAirlines();
        MekongAirlines mk = new MekongAirlines();

        mk.init(loai, diemDi, ngayDi, thangDi, diemDen, ngayVe, thangVe);
        vina.init(loai, diemDi, ngayDi, thangDi, diemDen, ngayVe, thangVe);

        try{ mk.TimKiem(); }
        catch (Exception) { }
        try { vina.TimKiem(); }
        catch (Exception) { };
    }
    [WebMethod(Description = "Them Chuyen Bay")]
    public void VinaTimKiem(string loai, string diemDi, string ngayDi, string thangDi, string diemDen, string ngayVe, string thangVe)
    {
        Airplane.lKhuHoi.Clear();
        Airplane.lInfo.Clear();
        XoaCB();
        XoaCBKH();
        ViNaAirlines mk = new ViNaAirlines();
        mk.init(loai, diemDi, ngayDi, thangDi, diemDen, ngayVe, thangVe);
        mk.TimKiem();
        
    }
    [WebMethod(Description = "Load all")]
    public void LoadAll()
    {
        for (int i = 0; i < Airplane.lKhuHoi.Count; i++)
        {
            ThemCBKH(Airplane.lKhuHoi[i]);
        }
        for (int i = 0; i < Airplane.lInfo.Count; i++)
        {
            ThemCB(Airplane.lInfo[i]);
        }
        for (int i = 0; i < Airplane.lcuoi.Count; i++)
        {
            ThemCB3(Airplane.lcuoi[i]);
        }
    }

    [WebMethod(Description = "Load Vina")]
    public void LoadViNa()
    {
        for (int i = 0; i < Airplane.lKhuHoi.Count; i++)
        {
            ThemCBKH(Airplane.lKhuHoi[i]);
        }
        for (int i = 0; i < Airplane.lInfo.Count; i++)
        {
            ThemCB(Airplane.lInfo[i]);
        }
    }
    [WebMethod(Description = "Tim kiem Mekong")]
    public void MekongTimkiem(string loai, string diemDi, string ngayDi, string thangDi, string diemDen, string ngayVe, string thangVe)
    {
        Airplane.lKhuHoi.Clear();
        Airplane.lInfo.Clear();
        XoaCB();
        XoaCBKH();
        MekongAirlines mk = new MekongAirlines();
        mk.init(loai, diemDi, ngayDi, thangDi, diemDen, ngayVe, thangVe);
        mk.TimKiem();
    }

    [WebMethod(Description = "Them Chuyen Bay")]
    public void ThemCB(thongtincb ttcb)
    {
        string sql = "Insert into thongtincb values ('" + ttcb.Hang + "','" + ttcb.DiemDi + "','" + ttcb.DiemDen + "','" + ttcb.ThoiGianDi + "','" + ttcb.ThoiGianDen + "','" + ttcb.Giave + "','" + ttcb.Chuyenbay + "')";
        SqlConnection conn = getConnection();
        SqlCommand commd = new SqlCommand(sql, conn);
        try
        {
            //Kiem tra trang thai cua connection
            if (conn.State.ToString() == "Closed")
            {
                conn.Open();
            }
            if (commd.ExecuteNonQuery() == 0)
            {

            }
        }
        catch (Exception)
        {

        }

    }

    [WebMethod(Description = "Xoa Het Chuyen Bay")]
    public void XoaCB()
    {
        string sql = "Delete from  thongtincb";
        SqlConnection conn = getConnection();
        SqlCommand commd = new SqlCommand(sql, conn);
        try
        {
            //Kiem tra trang thai cua connection
            if (conn.State.ToString() == "Closed")
            {
                conn.Open();
            }
            if (commd.ExecuteNonQuery() == 0)
            {

            }
        }
        catch (Exception)
        {

        }

    }

    [WebMethod(Description = "Danh sach chuyen bay ...")]
    public DataSet DanhSachCB()
    {
        string sql = "Select * from thongtincb";
        SqlConnection conn = getConnection();
        SqlDataAdapter adapter = new SqlDataAdapter(sql, conn);
        DataSet ds = new DataSet("thongtincb");
        try
        {
            adapter.Fill(ds);
        }
        catch (Exception)
        {
        }
        return (ds);
    }
    

    [WebMethod(Description = "Them Chuyen Bay")]
    public void ThemCBKH(thongtincb ttcb)
    {
        string sql = "Insert into thongtincbkh values ('" + ttcb.Hang + "','" + ttcb.DiemDi + "','" + ttcb.DiemDen + "','" + ttcb.ThoiGianDi + "','" + ttcb.ThoiGianDen + "','" + ttcb.Giave + "','" + ttcb.Chuyenbay + "')";
        SqlConnection conn = getConnection();
        SqlCommand commd = new SqlCommand(sql, conn);
        try
        {
            //Kiem tra trang thai cua connection
            if (conn.State.ToString() == "Closed")
            {
                conn.Open();
            }
            if (commd.ExecuteNonQuery() == 0)
            {

            }
        }
        catch (Exception)
        {

        }

    }

    [WebMethod(Description = "Xoa Het Chuyen Bay")]
    public void XoaCBKH()
    {
        string sql = "Delete from  thongtincbkh";
        SqlConnection conn = getConnection();
        SqlCommand commd = new SqlCommand(sql, conn);
        try
        {
            //Kiem tra trang thai cua connection
            if (conn.State.ToString() == "Closed")
            {
                conn.Open();
            }
            if (commd.ExecuteNonQuery() == 0)
            {

            }
        }
        catch (Exception)
        {

        }

    }

    [WebMethod(Description = "Danh sach chuyen bay ...")]
    public DataSet DanhSachCBKH()
    {
        string sql = "Select * from thongtincbkh";
        SqlConnection conn = getConnection();
        SqlDataAdapter adapter = new SqlDataAdapter(sql, conn);
        DataSet ds = new DataSet("thongtincbkh");
        try
        {
            adapter.Fill(ds);
        }
        catch (Exception)
        {
        }
        return (ds);
    }
    [WebMethod(Description = "Them Chuyen Bay c3")]
    public void ThemCB3(thongtincb ttcb)
    {
        string sql = "Insert into thongtincb3 values ('" + ttcb.Hang + "','" + ttcb.DiemDi + "','" + ttcb.DiemDen + "','" + ttcb.ThoiGianDi + "','" + ttcb.ThoiGianDen + "','" + ttcb.Giave + "','" + ttcb.Chuyenbay + "')";
        SqlConnection conn = getConnection();
        SqlCommand commd = new SqlCommand(sql, conn);
        try
        {
            //Kiem tra trang thai cua connection
            if (conn.State.ToString() == "Closed")
            {
                conn.Open();
            }
            if (commd.ExecuteNonQuery() == 0)
            {

            }
        }
        catch (Exception)
        {

        }

    }

    [WebMethod(Description = "Xoa Het Chuyen Bay c3")]
    public void XoaCB3()
    {
        string sql = "Delete from  thongtincb3";
        SqlConnection conn = getConnection();
        SqlCommand commd = new SqlCommand(sql, conn);
        try
        {
            //Kiem tra trang thai cua connection
            if (conn.State.ToString() == "Closed")
            {
                conn.Open();
            }
            if (commd.ExecuteNonQuery() == 0)
            {

            }
        }
        catch (Exception)
        {

        }

    }

    [WebMethod(Description = "Danh sach chuyen bay c3 ...")]
    public DataSet DanhSachCB3()
    {
        string sql = "Select * from thongtincb3";
        SqlConnection conn = getConnection();
        SqlDataAdapter adapter = new SqlDataAdapter(sql, conn);
        DataSet ds = new DataSet("thongtincb3");
        try
        {
            adapter.Fill(ds);
        }
        catch (Exception)
        {
        }
        return (ds);
    }
}